//
//  UIViewController+BarButtonExtension.m
//  CustomInterface
//
//  Created by tvo on 8/8/13.
//  Copyright (c) 2013 tvo. All rights reserved.
//

#import "UIViewController+BarButtonExtension.h"
#import "MMDrawerBarButtonItem.h"
#import "LSSearchViewController.h"

@implementation UIViewController (BarButtonExtension)
- (void)setListBarItem:(NSArray *)typeBarButtonItemList{
    for (NSNumber *type in typeBarButtonItemList) {
        UIBarButtonItem *item = nil;
        switch ([type integerValue]) {
            case BarItemButton_LeftMenu:
                item = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 40, 40) withImageNameStateNormal:@"btn_slideOutMenu" andImageNameStateHighlighted:@"" andWithAction:@selector(leftDrawerButtonPress:) withTag:[type integerValue]];
                self.navigationItem.leftBarButtonItem = item;
                break;
            case BarItemButton_Back:
                item = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 40, 40) withImageNameStateNormal:@"btn_back" andImageNameStateHighlighted:@"" andWithAction:@selector(goBack:) withTag:[type integerValue]];
                self.navigationItem.leftBarButtonItem = item;
                break;
            case BarItemButton_Search:
                item = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 40, 40) withImageNameStateNormal:@"btn_search" andImageNameStateHighlighted:@"" andWithAction:@selector(searchButtonPress:) withTag:[type integerValue]];
                self.navigationItem.rightBarButtonItem = item;
                break;
            case BarItemButton_Share:
                item = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 35, 35) withImageNameStateNormal:@"icon_share" andImageNameStateHighlighted:@"" andWithAction:@selector(clickShareButton:) withTag:[type integerValue]];
                self.navigationItem.rightBarButtonItem = item;
                break;
            case BarItemButton_Done:
            {
                UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
                doneButton.frame = CGRectMake(0, 0, 40, 40);
                [doneButton setTitle:@"Done" forState:UIControlStateNormal];
                doneButton.titleLabel.font = Roboto_Regular(14);
                [doneButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [doneButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                [doneButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
                [doneButton addTarget:self action:@selector(clickDoneButton:) forControlEvents:UIControlEventTouchUpInside];
                item = [[UIBarButtonItem alloc] initWithCustomView:doneButton];
                self.navigationItem.leftBarButtonItem = item;
                break;
            }
            case BarItemButton_Edit:
            {
                UIButton *editButton = [UIButton buttonWithType:UIButtonTypeCustom];
                editButton.frame = CGRectMake(0, 0, 40, 40);
                [editButton setTitle:@"Edit" forState:UIControlStateNormal];
                editButton.titleLabel.font = Roboto_Regular(14);
                [editButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [editButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                [editButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
                [editButton addTarget:self action:@selector(clickEditButton:) forControlEvents:UIControlEventTouchUpInside];
                item = [[UIBarButtonItem alloc] initWithCustomView:editButton];
                self.navigationItem.rightBarButtonItem = item;
                break;
            }
            case BarItemButton_Cancel:
            {
                UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
                cancelButton.frame = CGRectMake(0, 0, 44, 44);
                [cancelButton setTitle:@"Cancel" forState:UIControlStateNormal];
                cancelButton.titleLabel.font = Roboto_Regular(14);
                [cancelButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                [cancelButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
                [cancelButton addTarget:self action:@selector(clickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
                item = [[UIBarButtonItem alloc] initWithCustomView:cancelButton];
                self.navigationItem.leftBarButtonItem = item;
                break;
            }
            case BarItemButton_Save:
            {
                UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
                saveButton.frame = CGRectMake(0, 0, 40, 40);
                [saveButton setTitle:@"Save" forState:UIControlStateNormal];
                saveButton.titleLabel.font = Roboto_Regular(14);
                [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [saveButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
                [saveButton setTitleColor:[UIColor lightGrayColor] forState:UIControlStateSelected];
                [saveButton addTarget:self action:@selector(clickSaveButton:) forControlEvents:UIControlEventTouchUpInside];
                item = [[UIBarButtonItem alloc] initWithCustomView:saveButton];
                self.navigationItem.rightBarButtonItem = item;
                break;
            }
            case BarItemButton_Sort:
                item = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 40, 40) withImageNameStateNormal:@"btn_sort" andImageNameStateHighlighted:@"" andWithAction:@selector(sortButtonPress:) withTag:[type integerValue]];
                self.navigationItem.rightBarButtonItem = item;
                break;
            case BarItemButton_Sort_Search:
            {
                UIBarButtonItem *sortItem = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 40, 40) withImageNameStateNormal:@"btn_sort" andImageNameStateHighlighted:@"" andWithAction:@selector(sortButtonPress:) withTag:[type integerValue]];
                UIBarButtonItem *searchItem = [self createBarButtonItemWithFrame:CGRectMake(0, 0, 40, 40) withImageNameStateNormal:@"btn_search" andImageNameStateHighlighted:@"" andWithAction:@selector(searchButtonPress:) withTag:[type integerValue]];
                self.navigationItem.rightBarButtonItems = @[searchItem, sortItem];
                break;
            }
            default:
                break;
        }
    }
}

#pragma mark - BarButtonItem Action
- (void)leftDrawerButtonPress:(id)sender {
    [self.mm_drawerController toggleDrawerSide:MMDrawerSideLeft animated:YES completion:nil];
}

- (void)goBack:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)searchButtonPress:(id)sender {
    NSString *storyboardName = @"MainStoryboard~iPhone";
    if (IS_DEVICE_IPAD) {
        storyboardName = @"MainStoryboard~iPad";
    }
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
    LSSearchViewController *searchViewController = (LSSearchViewController *)[storyBoard instantiateViewControllerWithIdentifier:@"Search"];
    [self.mm_drawerController presentViewController:searchViewController animated:YES completion:NULL];
}

- (void)sortButtonPress:(id)sender {
    
}

- (void)clickShareButton:(id)sender {

}

- (void)clickDoneButton:(id)sender
{
    
}

- (void)clickEditButton:(id)sender
{
    
}

- (void)clickCancelButton:(id)sender
{
    
}

- (void)clickSaveButton:(id)sender
{
    
}
#pragma mark - BarButtonItem Created
- (UIBarButtonItem*)createBarButtonItemWithFrame:(CGRect)frame withImageNameStateNormal:(NSString*)imageNameStateNormal andImageNameStateHighlighted:(NSString*)imageNameStateHighlighted andWithAction:(SEL)action withTag:(NSInteger)tag {
    UIButton *buttonMenu = [[UIButton alloc] initWithFrame:frame];
    [buttonMenu setImage:[UIImage imageNamed:imageNameStateNormal] forState:UIControlStateNormal];
    [buttonMenu setImage:[UIImage imageNamed:imageNameStateHighlighted] forState:UIControlStateHighlighted];
    [buttonMenu addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
    buttonMenu.tag = tag;
    UIBarButtonItem *barButtonMenuItem = [[UIBarButtonItem alloc] initWithCustomView:buttonMenu];

    return barButtonMenuItem;
}

/*
- (UIBarButtonItem*)createBarButtonItemWithTitle:(NSString*)title withStyle:(UIBarButtonItemStyle)barButtonItemStyle withTarget:(id)target andWithAction:(SEL)action {
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:title style:barButtonItemStyle target:target action:action];

    return barButtonItem;
}

- (UIBarButtonItem*)creatBarButtonItemWithFrame:(CGRect)frame withTitle:(NSString*)title withTextColor:(UIColor*)textColor withFont:(UIFont*)font withTarget:(id)target andAction:(SEL)action {
    UIButton *buttonMenu = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonMenu setFrame:frame];
    [buttonMenu setTitle:title forState:UIControlStateNormal];
    [buttonMenu setTitleColor:textColor forState:UIControlStateNormal];
    [buttonMenu.titleLabel setFont:font];
    [buttonMenu.titleLabel setNumberOfLines:2];
    buttonMenu.showsTouchWhenHighlighted = YES;
    [buttonMenu addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return [[UIBarButtonItem alloc] initWithCustomView:buttonMenu];
}

- (UIBarButtonItem*)creatBarButtonItemWithFrame:(CGRect)frame withImageName:(NSString*)imageName withTitle:(NSString*)title withTextColor:(UIColor*)textColor withFont:(UIFont*)font withTarget:(id)target andAction:(SEL)action {
    UIButton *buttonMenu = [UIButton buttonWithType:UIButtonTypeCustom];
    [buttonMenu setFrame:frame];
    [buttonMenu setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    [buttonMenu setTitle:title forState:UIControlStateNormal];
    [buttonMenu setTitleColor:textColor forState:UIControlStateNormal];
    [buttonMenu.titleLabel setFont:font];
    [buttonMenu.titleLabel setNumberOfLines:2];
    buttonMenu.showsTouchWhenHighlighted = YES;
    [buttonMenu addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [buttonMenu setImageEdgeInsets:UIEdgeInsetsMake(-13, 8, 0, 0)];
    if (SD_IS_IOS7) {
        [buttonMenu setTitleEdgeInsets:UIEdgeInsetsMake(22, -17, 0, 0)];
    }
    else {
        [buttonMenu setTitleEdgeInsets:UIEdgeInsetsMake(22, -12, 0, 0)];
    }
    return [[UIBarButtonItem alloc] initWithCustomView:buttonMenu];
}
 */

@end
